<template>
  <!-- 体温单新增 -->
  <div>
    <div class="flex justify-between mb-2">
      <a-button type="primary" class="mr-2" @click="addOrder">新增</a-button>
      <div>
        <div class="absolute top-[-9999px] hidden">
          <a-button ref="printMe" v-print="{ id: 'printMe' }">护理记录单</a-button>
          <PrintView id="printMe" :print-data="printData" />
        </div>
        <a-button type="primary" ghost class="mr-2" @click="onPrint"><BaseIcon name="PrinterOutlined" :loading="printLoading" /> 护理记录单</a-button>
        <a-button v-if="auditStatus" :disabled="orderData.length <= 0" type="primary" class="" @click="onAudit('1')">审核</a-button>
        <a-button v-else class="" :disabled="orderData.length <= 0" @click="onAudit('0')">撤销审核</a-button>
      </div>
    </div>
    <div class="flex border">
      <div class="label">
        <div class="label-item flex-between items-center !h-[60px]">
          <span>体温单</span>
        </div>
        <div class="label-item">护理日期</div>
        <div class="label-item">护理时间</div>
        <div class="label-item">意识</div>
        <div class="label-item">体温</div>
        <div class="label-item">脉搏</div>
        <!-- <div class="label-item">收缩压</div>
        <div class="label-item">舒张压</div> -->
        <div class="label-item">血压</div>
        <div class="label-item">吸氧</div>
        <template v-if="showStyle">
          <div class="label-item">呼吸</div>
          <div class="label-item">饮食</div>
          <div class="label-item">血氧</div>
          <div class="label-item">护理</div>
          <div class="label-item">导管</div>
          <div class="label-item">措施</div>
          <div class="label-item">皮肤</div>
          <div class="label-item">伤口</div>
          <div class="label-item">指导</div>
        </template>
        <div class="label-item">血氧饱和度</div>
        <div class="label-item">视力(OD)</div>
        <div class="label-item">视力(OS)</div>
        <div class="label-item">眼压(OD)</div>
        <div class="label-item">眼压(OS)</div>
        <div class="label-item">瞳孔大小(OD)</div>
        <div class="label-item">瞳孔大小(OS)</div>
        <div class="label-item">双眼泪道冲洗</div>
        <div class="label-item">坠床跌倒评分</div>
        <div class="label-item">Barthel指数评分</div>
        <div class="label-item">Braden评分</div>
        <div class="label-item select-template">病情观察及护理措施</div>
        <div class="label-item">选择护理措施模板</div>
        <div class="label-item">记录人</div>
        <div class="label-item">审核人</div>
        <div class="label-item">审核时间</div>
      </div>
      <div class="flex w-full overflow-hidden overflow-x-scroll">
        <template v-for="(item, orderIndex) in orderData" :key="item.orderKey">
          <div class="content">
            <div class="content-item content-header flex-between items-center !h-[60px] content-item-width">
              <div class="flex-1 text-center text-base">{{ orderIndex + 1 }}</div>
              <div class="flex flex-col">
                <a-button type="text" size="large" :icon="h(MinusCircleOutlined)" @click="deleteOrder(item)" />
              </div>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="记录日期">
                <a-date-picker
                  v-model:value="item['nursingDate']"
                  :disabled="item['status'] === '1'"
                  placeholder="请输入"
                  value-format="YYYY-MM-DD"
                  suffix-icon=""
                />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="护理时间">
                <a-time-picker
                  v-model:value="item['nursingTime']"
                  :disabled="item['status'] === '1'"
                  placeholder="请输入"
                  value-format="HH:mm:ss"
                  suffix-icon=""
                />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="意识">
                <a-input v-model:value="item['awareness']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="体温">
                <a-input v-model:value="item['temperature']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="脉搏">
                <a-input v-model:value="item['pulse']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <!-- <div class="content-item content-item-width">
              <EditCell title="收缩压">
                <a-input v-model:value="item['systolicPressure']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="舒张压">
                <a-input v-model:value="item['diastolicPressure']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div> -->
            <div class="content-item content-item-width">
              <EditCell title="血压" class="!flex" :class="[item['status'] === '1' ? 'table-row-disabled' : '']">
                <a-input
                  v-model:value="item['systolicPressure']"
                  class="text-center"
                  :disabled="item['status'] === '1'"
                  placeholder="请输入"
                  @blur="saveRow(item)"
                />
                <span class="mx-1">/</span>
                <a-input
                  v-model:value="item['diastolicPressure']"
                  class="text-center"
                  :disabled="item['status'] === '1'"
                  placeholder="请输入"
                  @blur="saveRow(item)"
                />
                <span class="mr-1 ml-2">mmHg</span>
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="吸氧">
                <a-input v-model:value="item['oxygenUptake']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <template v-if="showStyle">
              <div class="content-item content-item-width">
                <EditCell title="呼吸">
                  <a-input v-model:value="item['breath']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
              <div class="content-item content-item-width">
                <EditCell title="饮食">
                  <a-input v-model:value="item['food']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
              <div class="content-item content-item-width">
                <EditCell title="血氧">
                  <a-input v-model:value="item['spo2']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
              <div class="content-item content-item-width">
                <EditCell title="护理">
                  <a-input v-model:value="item['nursing']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
              <div class="content-item content-item-width">
                <EditCell title="导管">
                  <a-input v-model:value="item['catheter']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
              <div class="content-item content-item-width">
                <EditCell title="措施">
                  <a-input v-model:value="item['method']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
              <div class="content-item content-item-width">
                <EditCell title="皮肤">
                  <a-input v-model:value="item['skin']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
              <div class="content-item content-item-width">
                <EditCell title="伤口">
                  <a-input v-model:value="item['wound']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
              <div class="content-item content-item-width">
                <EditCell title="指导">
                  <a-input v-model:value="item['guidance']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
                </EditCell>
              </div>
            </template>
            <div class="content-item content-item-width">
              <EditCell title="血氧饱和度">
                <a-input v-model:value="item['oxygenBlood']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="视力(OD)">
                <a-input v-model:value="item['visionOd']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="视力(OS)">
                <a-input v-model:value="item['visionOs']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="眼压(OD)">
                <a-input v-model:value="item['pressureOd']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="眼压(OS)">
                <a-input v-model:value="item['pressureOs']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <!-- <div class="content-item flex">
            <EditCell title="血压" class="!w-2/5">
              <a-input v-model:value="item['ssy']" placeholder="收缩压" />
            </EditCell>
            <div class="w-1/5 absolute text-center left-[40%] text-slate-400">/</div>
            <EditCell title="血压" class="!w-2/5 !left-[60%]">
              <a-input v-model:value="item['szy']" placeholder="舒张压" />
            </EditCell>
          </div> -->
            <div class="content-item content-item-width">
              <EditCell title="瞳孔大小(OD)">
                <a-input v-model:value="item['pupilOd']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="瞳孔大小(OS)">
                <a-input v-model:value="item['pupilOs']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="双眼泪道冲洗">
                <a-input v-model:value="item['lacrimalFlushing']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="坠床跌倒评分">
                <a-input v-model:value="item['fallRate']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="Barthel指数评分">
                <a-input v-model:value="item['barthelRate']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="Braden评分">
                <a-input v-model:value="item['bradenRate']" :disabled="item['status'] === '1'" placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item select-template content-item-width">
              <EditCell title="病情观察及护理措施">
                <a-textarea
                  v-model:value="item['diseaseObservation']"
                  :disabled="item['status'] === '1'"
                  :rows="8"
                  placeholder="请输入"
                  @blur="saveRow(item)"
                />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <a-button type="link" size="small" :disabled="item['status'] === '1'" @click="onClickTemplate(item)"> 选择模板 </a-button>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="记录人">
                <SelectTable
                  v-model:value="item['recordEmpName']"
                  :disabled="item['status'] === '1'"
                  custom-input
                  type="员工"
                  page-size="999"
                  @select="
                    (select) => {
                      item['recordEmpId'] = select?.empId
                      item['recordEmpName'] = select?.empName
                      saveRow(item)
                    }
                  "
                />
                <!-- <a-input v-model:value="item['createUser']" disabled placeholder="请输入" @blur="saveRow(item)" /> -->
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="审核人">
                <a-input v-model:value="item['auditEmpName']" disabled placeholder="请输入" @blur="saveRow(item)" />
              </EditCell>
            </div>
            <div class="content-item content-item-width">
              <EditCell title="审核时间">
                <a-input v-model:value="item['auditTime']" disabled placeholder="请输入" />
              </EditCell>
            </div>
          </div>
        </template>
      </div>
    </div>
    <div class="m-2 flex justify-end">
      <a-button type="primary" @click="onSave"> 保存</a-button>
    </div>
    <Selection v-model:open="editOpen" @success="onChangeTemplate" />
  </div>
</template>
<script setup>
import { ref, h, watchEffect } from 'vue'
import { MinusCircleOutlined } from '@ant-design/icons-vue'

import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { post } from '@/utils/request'
import useUserStore from '@/store/user'
import Selection from './selection.vue'
import PrintView from './components/PrintView.vue'

const { empName, empId } = useUserStore()

const props = defineProps({
  id: {
    type: String,
    default: ''
  }
})

const showStyle = getSystemConfig('INP_PRINT_NURSE_CARE_STYLE') === '2' //住院打印护理记录单样式
const auditStatus = ref(true)
const editOpen = ref(false)
const orderData = ref([])
const loading = ref(false)
const selectTemplate = ref({})
const newRow = ref([])
const printData = ref({})
const printMe = ref()
const onSave = () => {
  if (!props.id) return message.warning('暂无患者')
  newRow.value = orderData.value.filter((item) => item.isNew)
  if (newRow.value.length > 0) {
    newRow.value.forEach((item, index) => {
      fetchSave(item, index === newRow.value.length - 1 ? true : undefined)
    })
  } else {
    message.success('操作成功')
    getData()
  }
}
const saveRow = (row) => {
  if (!row.isNew) {
    fetchSave(row)
  }
}
const onClickTemplate = (item) => {
  selectTemplate.value = item
  editOpen.value = true
}
const onAudit = async (status) => {
  if (!props.id) return message.warning('暂无患者')
  const { code, msg } = await post('inp/InpNursingRecord/doAuditInpNursingRecord', {
    inpLsh: props.id,
    status
  })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')
  getData()
}
const printLoading = ref(false)
//护理记录单
const onPrint = async () => {
  if (!props.id) return message.warning('暂无患者')
  printLoading.value = true
  const { code, msg, data } = await post('inp/InpNursingRecord/printInpNursingRecord', { inpLsh: props.id })
  printLoading.value = false
  if (code !== 0) return message.error(msg)
  if (getSystemConfig('NAVYHIS_MODE') === '1') {
    printData.value = JSON.parse(data)
    setTimeout(() => {
      setPrintSize('A4', 'landscape')
      printMe.value.$el.click()
    }, 1000)
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
}

const onChangeTemplate = (row) => {
  orderData.value.forEach((item) => {
    if (item.id === selectTemplate.value.id) {
      item.diseaseObservation = row
      fetchSave(item, true)
    }
  })
}
const fetchSave = async (row, status) => {
  const { code, msg } = await post('inp/InpNursingRecord/doSaveInpNursingRecord', {
    inpLsh: props.id,
    ...row,
    bp: (row.systolicPressure ?? '') + '/' + (row.diastolicPressure ?? '')
  })
  if (code !== 0) return message.error(msg || '保存失败')
  if (status) {
    message.success(msg || '保存成功')
    getData()
  }
}
const deleteOrder = (row) => {
  if (row.isNew) {
    orderData.value = orderData.value.filter((item) => row.orderKey !== item.orderKey)
  } else {
    removeOrder(row)
  }
}
const removeOrder = async (row) => {
  const { msg, code } = await post('inp/InpNursingRecord/doCancelInpNursingRecord', { id: row.id })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success(msg || '操作成功')
  getData()
}
const addOrder = () => {
  if (!props.id) return message.warning('暂无患者')
  const orderKey = `order-${orderData.value.length}`
  orderData.value.push({
    orderKey,
    nursingDate: dayjs().format('YYYY-MM-DD'),
    isNew: true,
    nursingTime: dayjs().format('HH:mm:ss'),
    recordEmpName: empName,
    recordEmpId: empId
  })
}
//获取数据
const getData = async () => {
  if (!props.id) return message.warning('暂无患者')
  loading.value = true
  auditStatus.value = true
  const { data, code, msg } = await post('inp/InpNursingRecord/getInpNursingRecordList', { inpLsh: props.id })
  loading.value = false
  orderData.value = []
  if (code !== 0) return message.error(msg || '获取数据失败')
  if (data.length === 0) {
    orderData.value = [
      { orderKey: 'order-0', nursingDate: dayjs().format('YYYY-MM-DD'), isNew: true, nursingTime: dayjs().format('HH:mm:ss'), createUser: empName }
    ]
    auditStatus.value = true
  } else {
    orderData.value = data.map((item) => {
      if (item.status === '1') {
        auditStatus.value = false
      } else {
        auditStatus.value = true
      }
      return {
        ...item,
        orderKey: `order-${item.id}`,
        systolicPressure: item.bp?.split('/')?.[0] || '',
        diastolicPressure: item.bp?.split('/')?.[1] || ''
      }
    })
  }
}

watchEffect(() => props.id && getData())
</script>
<style lang="less" scoped>
.label-item {
  padding: 0 8px;
  background-color: #00000008;
}
.content-header {
  background-color: #00000008;
}
.label-item,
.content-item {
  position: relative;
  width: 160px;
  height: 40px;
  line-height: 40px;
  border-right: 1px solid #e5e7eb;
  border-bottom: 1px solid #e5e7eb;
}
.content-item-width {
  width: 320px;
}
.label-item.select-template,
.content-item.select-template {
  height: 160px;
}
.label-item:last-child,
.content-item:last-child {
  border-bottom: none;
}
</style>
